from selenium import webdriver
import requests
import time
import pandas as pd

# 打开好看视频的“美食”频道页面
browser = webdriver.Chrome()
browser.get('https://haokan.baidu.com/tab/meishi_new')

# 等待用户登录
time.sleep(60)

# 获取Cookie并转换数据格式
cookie_dict = {}
for item in browser.get_cookies():
    cookie_dict[item['name']] = item['value']
browser.quit()

# 给出接口地址、动态参数、请求头
url = 'https://haokan.baidu.com/haokan/ui-web/video/rec'
params = {'tab': 'meishi_new', 'act': 'pcFeed', 'pd': 'pc', 'num': 20}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'}

# 爬取数据
data_list = []
for i in range(3):
    # 携带动态参数和Cookie对接口地址发起请求
    response = requests.get(url=url, params=params, headers=headers, cookies=cookie_dict)
    # 从响应对象中解析JSON格式数据
    json_data = response.json()
    # 从JSON格式数据中提取视频数据
    video_list = json_data['data']['response']['videos']
    # 提取所需字段并进行数据清洗
    for video in video_list:
        data = {}
        data['标题'] = video['title']
        data['时长'] = video['duration']
        data['播放量'] = int(eval(video['fmplaycnt_2'].replace('万', 'e4')))
        data['评论数'] = int(video['fmcomment'])
        data['点赞数'] = int(video['fmlike'])
        data['网址'] = video['play_url']
        # 将提取的数据添加到列表中
        data_list.append(data)
    # 适当等待，以免触发反爬
    time.sleep(3)

# 整理和导出数据
df = pd.DataFrame(data_list)
df.to_csv('好看视频.csv', index=False, encoding='utf-8-sig')
